package main

import (
	"common/log"
	"common/process"
	"euplink"
	"fmt"
	"github.com/spf13/cobra"
	"github.com/wuzfei/go-helper/unit"
)

var listObjectsCmd = &cobra.Command{
	Use:   "list_objects",
	Short: "文件列表",
	Long:  "euplink list_objects",
	RunE:  cmdListObjects,
}

func cmdListObjects(cmd *cobra.Command, args []string) error {
	ctx, _ := process.Ctx(cmd)
	zapLog := log.NewZapLog(&listObjectConfig.Log)
	//runConfig.Project.LocalDeviceId = runConfig.LocalDeviceId
	//runConfig.Project.RootKey = rootKeyToMd5(runConfig.RootKey)
	//runConfig.Project.ApiKey = runConfig.ApiKey
	prj, err := euplink.NewProject(ctx, listObjectConfig.Project, zapLog)
	if err != nil {
		return fmt.Errorf("project init err: %s", err)
	}
	list, err := prj.ListObject(ctx, runConfig.Project.DefaultBucket, listObjectConfig.Pre, listObjectConfig.Cursor)
	//list, err := prj.ListObject(ctx, listObjectConfig.Project.DefaultBucket, "", "")
	if err != nil {
		fmt.Println("获取列表失败：", err)
	}
	for _, v := range list {
		fmt.Printf("文件名：%s, 大小：%s\n", v.ObjectKey, unit.ByteFormat(v.Size, 3))
	}
	return nil
}
